from database.db_handler import MysqlHander
from common.my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
from common.market_data import MarketData
import glob
import jieba
import re
import threading
import hashlib
import json
from database.db_business import DbBusiness

'''
宏观数据：发展改革委员会
'''

class DongfangcaifuJiejin:
    def __init__(self):
        self.entity = EntityData()
        self.business = DbBusiness()
        self.code_ex = {}
        self.code_name = {}
    
    def get_url(self, code, exchange_id):
        ex = ""
        if exchange_id == 1:
            ex = "SH"
        elif exchange_id == 2:
            ex = "SZ"
        elif exchange_id == 3:
            ex = "BJ"
        return "http://f10.eastmoney.com/CapitalStockStructure/PageAjax?code=" + ex + code
    
    def get_online_data(self, url, code):
        proxy = self.business.query_proxy()
        json_data = json.loads(MyHttp.http_json_data_v2(url, proxy))
        
        keys = json_data.keys()
        if "xsjj" not in keys:
            return False
        for d in json_data["xsjj"]:
            data_title = self.code_name[code] + "解禁" + str(d["LIFT_NUM"]) + "股,占总股份数:" + str(d["TOTAL_SHARES_RATIO"]) + "%"
            data_date = d["LIFT_DATE"][0:10]
            data_url = ""
            self.market.add_market_data(data_date, 2301, data_title, data_url, "东方财富网")
        return True

    
    def get_data(self):
        self.code_ex = self.business.query_stock_code_exchange()
        self.code_name = self.business.query_stock_code_name()
        for k,v in self.code_ex.items():
            url = self.get_url(k, v)
            self.get_online_data(url, k)


if __name__ == '__main__':
    d = DongfangcaifuJiejin()
    d.get_data()
